package Xmlcompare;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Properties;
 
/**
 * Contains some methods to list files and folders from a directory
 *
 * @author Loiane Groner
 * http://loiane.com (Portuguese)
 * http://loianegroner.com (English)
 */
public class CompareUtil {
 
    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
    public void listFilesAndFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            System.out.println(file.getName());
        }
    }
 
    /**
     * List all the files under a directory
     * @param directoryName to be listed
     * @throws IOException 
     */
    public void listFiles(String directoryName) throws IOException  {
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
        String a1 [] = null;
        for (File file : fList){
            if (file.isFile()){
            	if(file.getName().substring(file.getName().lastIndexOf(".")+1,file.getName().length()).equalsIgnoreCase("xml"))
            	{
            		//tcgen.main1(directoryName+File.separator+file.getName(),dest,tc);// to generate tc
            		//CompareXml.main1(directoryName+File.separator+file.getName(),dest,Key);// to generate tc
            	}
            	else
            	{
            		System.out.println(directoryName+File.separator+file.getName());
            	}
            	/*
            	a1[0]=directoryName+File.separator+file.getName();
            	a1[1]=directoryName+File.separator+"Result"+file.getName();
            	a1[2]=directoryName+File.separator+"Log"+file.getName();*/
            	
                //count.main1(directoryName+File.separator+file.getName());
            	// System.out.println(directoryName+File.separator+file.getName());
            }
        }
    }
 
    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }
 
    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
 public static String dest1=null,Key=null,dest2=null,dest=null,xlog;
 public static PrintWriter outLog=null;
    public static void main (String[] args) throws IOException{
 
        CompareUtil listFilesUtil = new CompareUtil();
 
        final String directoryLinuxMac ="/Users/loiane/test";
 
        //Windows directory example
        final String directoryWindows1 =args[0];
        final String directoryWindows2 =args[1];
        Key=args[2];
 System.out.println(directoryWindows1);
 String directoryWindows=directoryWindows1.substring(0, directoryWindows1.lastIndexOf(File.separator));
 xlog=directoryWindows+File.separator+"logs"+Math.round(Math.random()*10000);
 new File(xlog).mkdirs();
 dest1=directoryWindows+File.separator+"out"+Math.round(Math.random()*10000);
 dest=dest1;
 if(new File(dest).exists())
 {
	 new File(dest).delete();
	 
 }
 new File(dest).mkdirs();
 listFilesUtil.listFiles(directoryWindows1);
 directoryWindows=directoryWindows2.substring(0, directoryWindows2.lastIndexOf(File.separator));
 dest2=directoryWindows+File.separator+"out"+Math.round(Math.random()*10000);
 dest=dest2;
 if(new File(dest).exists())
 {
	 new File(dest).delete();
	 
 }
 new File(dest).mkdirs();
 
        
        listFilesUtil.listFiles(directoryWindows2);
        
        File directory = new File(dest);
        
        //get all the files from a directory
        File[] fList = directory.listFiles();
       
        for (File file : fList){
            if (file.isFile()){
            	if(file.getName().substring(file.getName().lastIndexOf(".")+1,file.getName().length()).equalsIgnoreCase("properties"))
            	{
            		//tcgen.main1(directoryName+File.separator+file.getName(),dest,tc);// to generate tc
            		
            		compare(dest1+File.separator+file.getName(),dest2+File.separator+file.getName());
            	}
            	else
            	{
            		System.out.println(dest+File.separator+file.getName());
            	}
            	
            }
        }
        
    }
    
    
    	
   
    public static void compare(String file1,String file2) throws IOException
    { 
    	File fileDir = new File(file1);
    	Properties prop1 = new Properties();
    	Properties prop2 = new Properties();
    	prop1.load(new FileInputStream(file1));
    	prop2.load(new FileInputStream(file2));
    	String Value1,Value2; 
    	if(prop1.equals(prop2))
    	{
    		System.out.println("equal12323");
    	}
    	
    	
   	 String [] Xpath;
		BufferedReader in = new BufferedReader(
		   new InputStreamReader(
                      new FileInputStream(fileDir), "UTF8"));
 
		String str;
 
		while ((str = in.readLine()) != null) {
			Xpath=str.split("=");
			Value1=prop1.getProperty(Xpath[0]);
			Value2=prop2.getProperty(Xpath[0]);
		    if(Value1.equals(Value2))
		    {
		    	
		    }
		    else
		    {
		    	System.out.println("Path="+Xpath[0]+"\n  File Name : "+file1+"\n Value : "+Value1+"\n  File Name : "+file2+"\n Value : "+Value2);
		    }
			
			
		}
 
                in.close();
    	
    	
    }
}